using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Threading;
using log4net;
using System.Reflection;

namespace ICeFramework.Win.UI.ExceptionHandler
{
    public class ExceptionHandler
    {
        private static ErrFrm ErrFrm = new ErrFrm();
        private static readonly ILog _log = LogManager.GetLogger(typeof(ExceptionHandler));

        public static void DisplayMessage(Exception ex)
        {
            DisplayMessage(ex, "");
        }

        public static void DisplayMessage(Exception ex, string userName)
        {
            StringBuilder sb = new StringBuilder();
            string message = string.Empty;

            message = ex.Message;

            // Show Developers extra information about the error
            // Line numbers, StackTrace, etc.
            #if DEBUG
            {
                {
                    string nl = Environment.NewLine;

                    sb.Append(ex.GetType().Name + " Information:" + nl);
                    sb.Append("Message: " + ex.Message + nl);
                    sb.Append("Source: " + ex.Source + nl);
                    sb.Append("Stack Trace: " + ex.StackTrace + nl);

                    if (ex.InnerException != null)
                    {
                        sb.Append(nl + "Inner Exception Info:" + nl);
                        sb.Append("Message: " + ex.InnerException.Message + nl);
                        sb.Append("Source: " + ex.InnerException.Source + nl);
                        sb.Append("Stack Trace: " + ex.InnerException.StackTrace + nl);
                    }
                }
            }
            #endif

            _log.Error(ex.Message, ex);
            ShowError(message, sb.ToString());
        }

        public static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            DisplayMessage(e.Exception);            
        }

        public static void ShowError(Exception ex)
        {
            ErrFrm.label1.Text = ex.Message;
            ErrFrm.textBox1.Text = ex.StackTrace;
            ErrFrm.Height = 125;

            int w = ErrFrm.label1.Left + ErrFrm.label1.Width + 3;
            if (w > 503)
            {
                ErrFrm.Width = ErrFrm.label1.Left + ErrFrm.label1.Width + 10;                

            }
            ErrFrm.ShowDialog();
        }

        private static void ShowError(string message, string stackTrace)
        {
            ErrFrm.label1.Text = message;
            ErrFrm.textBox1.Text = stackTrace;
            ErrFrm.Height = 125;

            int w = ErrFrm.label1.Left + ErrFrm.label1.Width + 3;
            if (w > 503)
            {
                ErrFrm.Width = ErrFrm.label1.Left + ErrFrm.label1.Width + 10;

            }
            ErrFrm.ShowDialog();
        }
    }
}
